namespace _146 {

class LRUCache {
    data = new Map<number, number>() 
    lru = new Set<number>() 
    constructor(private capacity: number) {

    }
    get(key: number): number {
        if (this.data.has(key)) {
            if (this.lru.has(key)) this.lru.delete(key)
            this.lru.add(key)
            return this.data.get(key)
        }
        return -1
    }
    set(key: number, value: number) {
        if (this.data.has(key)) {
            this.lru.delete(key)
        }
        else if (this.data.size === this.capacity) {
            let k = this.lru.values().next().value
            this.data.delete(k)
            this.lru.delete(k)
        }
        this.data.set(key, value)
        this.lru.add(key)
    }
}

var sut = new LRUCache(2)

sut.set(2,1)
sut.set(1,1)
sut.set(2,3)
sut.set(4,1)

console.assert( sut.get(1) == -1)
console.assert( sut.get(2) == 3)

sut = new LRUCache(2)
console.assert( sut.get(2) == -1)
sut.set(2,6)
console.assert( sut.get(1) == -1)
sut.set(1,5)
sut.set(1,2)
console.assert( sut.get(1) == 2 )
console.assert( sut.get(2) == 6)

}